﻿using DadayunApi.Model;
using KhtApi.BLL;
using KhtApi.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KhtMaintain
{
    public partial class FrmBomFixProductInfo : Form
    {
        FormBLL bll = null;
        ReportBLL prdBll = null;
        List<Bom> boms = null;
        public FrmBomFixProductInfo(AppAuthBase authBase, string baseUri)
        {
            this.bll = new FormBLL(authBase,baseUri);
            prdBll = new ReportBLL(authBase, baseUri);
            InitializeComponent();
        }

       
        private void btnSearch_Click(object sender, EventArgs e)
        {
            ProductInfo product = prdBll.GetProductInfo(txtPrdCodeSearch.Text);
            if(product!=null)
            {
                txtCat1.Text = product.Catagory1;
                txtCat2.Text = product.Catagory2;
                txtCat3.Text = product.Catagory3;
                txtItemCode.Text = txtPrdCodeSearch.Text;
                txtName.Text = product.Name;
                txtModelNo.Text = product.ModelNo;
                txtOringinal.Text = product.OriginCn;
                txtSpec.Text = product.Spec;
                boms = bll.GetBoms(txtPrdCodeSearch.Text);
                dgvData.DataSource = null;
                dgvData.DataSource = boms;
                staInfo.Text = "搜索到" + boms.Count + "条BOM";
                MarkErrors();
            }
            else
            {
                staInfo.Text = "未找到指定的产品信息！";
            }
           
        }
        /// <summary>
        /// 标识BOM单中产品信息与产品信息表不相符的BOM单，并用红色标识出来
        /// </summary>
       private void MarkErrors()
        {
            for (int i = 0; i < dgvData.Rows.Count; i++)
            {
                bool mark = false;
                DataGridViewRow r = dgvData.Rows[i];
                if (r.Cells["PrdName"].Value.ToString() != txtName.Text) mark = true;
                if (r.Cells["PrdModelNo"].Value.ToString() != txtModelNo.Text) mark = true;
                if (r.Cells["PrdSpec"].Value.ToString() != txtSpec.Text) mark = true;
                if (r.Cells["PrdOringinal"].Value == null) mark = true;
                else  if (r.Cells["PrdOringinal"].Value.ToString() != txtOringinal.Text) mark = true;
                if(mark)
                {
                    dgvData.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                }
            }
            staInfo.Text += "。信息有差异的已经用红色底色标识,请选择要修复的BOM";
        }

        private void btnFixSelected_Click(object sender, EventArgs e)
        {
            int sus = 0, fail = 0, pass = 0;
            foreach (DataGridViewRow r in dgvData.SelectedRows)
            {
                Bom bom = bll.GetBom(r.Cells["InstanceId"].Value.ToString());
                if(bom.ModelNo==txtModelNo.Text && bom.Name==txtName.Text && bom.Spec==txtSpec.Text && bom.OriginCn==txtOringinal.Text)
                {
                    pass++;
                    continue;
                }
                bom.ModelNo = txtModelNo.Text;
                bom.Name = txtName.Text;
                bom.Spec = txtSpec.Text;
                bom.OriginCn = txtOringinal.Text;
                if (bll.FixBomProduct(bom))
                {
                    sus++;
                }
                else
                {
                    fail++;
                    string msg = "正在修复物料代码：" + bom.ItemCode + ", BOM编码：" + bom.BomId;
                    msg += "\r\n系统返回的信息为：\r\n" + bll.ResponseJson;
                    msg += "\r\n继续修复吗？";
                    var ans = MessageBox.Show(msg, "修复过程发生错误", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    if (ans == DialogResult.No) return;
                    continue;
                }
                staInfo.Text = "成功修复" + sus + "条，失败" + fail + "条，跳过："+pass+"条";
                Application.DoEvents();
            }
            staInfo.Text += "。修复已完成！";
        }

        private void FrmBomFixProductInfo_Load(object sender, EventArgs e)
        {
            dgvData.AutoGenerateColumns = false;
        }

        private void btnFixAll_Click(object sender, EventArgs e)
        {//提取所有BOM-->逐条查询BOM的产品信息--更新
            if (MessageBox.Show("即将更新所有BOM单的产品信息。此操作耗时较长，建议在网络空闲时段内执行此操作。确定马上执行吗？", "操作确认",
                MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return;
            staInfo.Text = "正在读取所有BOM单，请稍后";Application.DoEvents();
            List<Bom> boms = bll.GetBoms();
            dgvData.DataSource = null;
            dgvData.DataSource = boms;
            dgvData.MultiSelect = false;
            string msg = "查询到" + boms.Count + "条BOM";
            staInfo.Text = msg + ",马上逐条更新";
            int sus = 0, fail = 0,pass=0;
            int n = 0;
            foreach (Bom b in boms)
            {
                dgvData.Rows[n++].Selected = true;
                ProductInfo p = prdBll.GetProductInfo(b.ItemCode);
                if(b.Name!=p.Name||b.Spec!=p.Spec||b.ModelNo!=p.ModelNo||b.Origin != p.Origin)
                {
                    b.Name = p.Name;
                    b.Spec = p.Spec;
                    b.ModelNo = p.ModelNo;
                    b.Origin = p.Origin;
                    if (bll.FixBomProduct(b))
                        sus++;
                    else
                    {
                        fail++;
                        msg = "正在修复物料代码：" + b.ItemCode + ", BOM编码：" + b.BomId;
                        msg += "\r\n系统返回的信息为：\r\n" + bll.ResponseJson;
                        msg += "\r\n继续修复吗？";
                        var ans = MessageBox.Show(msg, "修复过程发生错误", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                        if (ans == DialogResult.No) return;
                        continue;
                    }
                    
                }
                else
                {
                    pass++;
                }
                staInfo.Text = "进度："+n+"/"+boms.Count+"，成功修复" + sus + "条，失败" + fail + "条，跳过：" + pass + "条";
                Application.DoEvents();
            }
            staInfo.Text = "修复完成！";
        }

        private void dgvData_SelectionChanged(object sender, EventArgs e)
        {
            btnFixSelected.Enabled = dgvData.SelectedRows.Count > 0;
        }
    }
}
